package com.ruoyi.psi.inventory.record.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.produce.process.receiving.domain.ReceivingReportDto;
import com.ruoyi.produce.process.receiving.mapper.ReceivingReportMapper;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDomain;
import com.ruoyi.psi.base.warehouse.domain.WarehouseListDto;
import com.ruoyi.psi.base.warehouse.mapper.WarehouseMapper;
import com.ruoyi.psi.inventory.record.domain.PurchaseOrderStockInRecord;
import com.ruoyi.psi.inventory.record.domain.StockInRecord;
import com.ruoyi.psi.inventory.record.domain.StockInRecordCond;
import com.ruoyi.psi.inventory.record.mapper.StockInRecordMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 入库记录
 * 业务逻辑
 */
@Service
public class StockInRecordServiceImpl implements IStockInRecordService {
    @Autowired
    private StockInRecordMapper stockInRecordMapper;
    @Autowired
    private WarehouseMapper warehouseMapper; //仓库
    @Autowired
    private IdUtil idUtil;
    @Autowired
    private ReceivingReportMapper receivingReportMapper;

    /**
     * 根据搜索条件所有入库记录
     *
     * @param cond 搜索条件
     * @return
     */
    @Override
    public List<StockInRecord> selectStockInRecordList(StockInRecordCond cond) {
        return stockInRecordMapper.selectStockInRecordList(cond);
    }

    /**
     * 新增入库记录
     *
     * @param stockInRecord
     * @return
     */
    @Override
    public int insertStockInRecord(StockInRecord stockInRecord) {
        String stockInId = idUtil.createStockInId();
        if (StringUtils.isNotNull(stockInRecordMapper.selectStockInRecordById(stockInId))) {
            //存在
            return 0;
        }
        //不存在
        stockInRecord.setStockInId(stockInId); //主键添加
        stockInRecordMapper.insertStockInRecord(stockInRecord);
        // 操作库存
        boolean isExistFlag = false; // 是否存在的标志位，默认不存在
        double productNumber = 0.0; //产品数量
        List<WarehouseListDto> warehouseList = warehouseMapper.selectWarehouseListListById(stockInRecord.getWarehouseId()); // 查询该仓库的产品列表
        for (WarehouseListDto s : warehouseList) {
            if (stockInRecord.getProduceId().equals(s.getProduceId())) {
                isExistFlag = true;
                productNumber = s.getProductNumber();
                break;
            }
        }
        WarehouseListDomain warehouseListDomain = new WarehouseListDomain(); // 仓库子表的对象
        warehouseListDomain.setWarehouseId(stockInRecord.getWarehouseId());
        warehouseListDomain.setProduceId(stockInRecord.getProduceId());
        if (isExistFlag) {
            // 存在，更新仓库子表
            //入库数量 = 原有的产品数量 + 入库数量
            warehouseListDomain.setProductNumber(productNumber + stockInRecord.getWarehousingNumber());
            warehouseMapper.updateWarehouseList(warehouseListDomain);
        } else {
            // 不存在，新增仓库子表
            warehouseListDomain.setProductNumber(stockInRecord.getWarehousingNumber()); //入库数量
            warehouseMapper.insertWarehouseList(warehouseListDomain);
        }
        return 1;
    }

    /**
     * 修改入库记录
     *
     * @param stockInRecord
     * @return
     */
    @Override
    public int updateStockInRecord(StockInRecord stockInRecord) {
        if (!StringUtils.isNotNull(stockInRecordMapper.selectStockInRecordById(stockInRecord.getStockInId()))) {
            //不存在
            return 0;
        }
        // 操作库存
        // 更新前库存对象
        StockInRecord stockInRecordOld = stockInRecordMapper.selectStockInRecordById(stockInRecord.getStockInId());
        double productNumber = 0.0; //产品数量
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(stockInRecord.getWarehouseId());// 查询该仓库的产品列表
        for (WarehouseListDto s : warehouseListDtos) {
            if (stockInRecord.getProduceId().equals(s.getProduceId())) {
                productNumber = s.getProductNumber();
                break;
            }
        }
        WarehouseListDomain warehouseListDomain = new WarehouseListDomain(); // 仓库子表的对象
        warehouseListDomain.setWarehouseId(stockInRecord.getWarehouseId());
        warehouseListDomain.setProduceId(stockInRecord.getProduceId());
        //入库数量 = 原有的产品数量 - 原入库数量 + 新入库数量
        warehouseListDomain.setProductNumber(productNumber - stockInRecordOld.getWarehousingNumber() + stockInRecord.getWarehousingNumber());
        warehouseMapper.updateWarehouseList(warehouseListDomain);
        stockInRecordMapper.updateStockInRecord(stockInRecord);
        return 1;
    }

    /**
     * 删除入库记录
     *
     * @param stockInRecord
     * @return
     */
    @Override
    public int updateStockInRecordDeleteFlag(StockInRecord stockInRecord) {
        if (!StringUtils.isNotNull(stockInRecordMapper.selectStockInRecordById(stockInRecord.getStockInId()))) {
            //不存在
            return 0;
        }
        // 操作库存
        List<WarehouseListDto> warehouseListDtos = warehouseMapper.selectWarehouseListListById(stockInRecord.getWarehouseId());// 查询该仓库的产品列表
        WarehouseListDomain warehouseListDomain = new WarehouseListDomain();
        warehouseListDomain.setWarehouseId(stockInRecord.getWarehouseId());
        warehouseListDomain.setProduceId(stockInRecord.getProduceId());
        for (WarehouseListDto s : warehouseListDtos) {
            if (stockInRecord.getProduceId().equals(s.getProduceId())) {
                //删除时，产品库存数 = 原有库存 - 该产品入库时的库存数
                warehouseListDomain.setProductNumber(s.getProductNumber() - stockInRecord.getWarehousingNumber());
                break;
            }
        }
        warehouseMapper.updateWarehouseList(warehouseListDomain);
        stockInRecordMapper.updateStockInRecordDeleteFlag(stockInRecord);
        return 1;
    }

    /**
     * 根据搜索条件所有采购订单入库记录
     * @param cond 搜索条件
     * @return
     */
    @Override
    public List<PurchaseOrderStockInRecord> selectPurchaseOrderStockInRecordList(StockInRecordCond cond) {
        List<PurchaseOrderStockInRecord> purchaseOrderStockInRecords = stockInRecordMapper.selectPurchaseOrderStockInRecordList(cond);
        for (PurchaseOrderStockInRecord s : purchaseOrderStockInRecords) {
            ReceivingReportDto receivingReportDto = receivingReportMapper.selectReceivingReportById(s.getAssociationId());
            s.setPurchaseOrderId(receivingReportDto.getWorkSheetId()); //设置采购订单
        }
        return purchaseOrderStockInRecords;
    }
}
